package com.nuaa.qianru.Health;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

class DataRead{

		public double[][] getNumber1() throws Exception{
			FileInputStream fis = null;
			double[][] blh=null;
			try {
				File file;
				fis = new FileInputStream("qqai.xlsx");
				XSSFWorkbook sheets = new XSSFWorkbook(fis);
				XSSFSheet sheet = sheets.getSheet("Sheet1");
				int rows = sheet.getPhysicalNumberOfRows();
				XSSFRow row = sheet.getRow(1);
				int columns = row.getPhysicalNumberOfCells();
				System.out.println(rows);
				blh=new double[rows][columns];
				for (int i = 0; i < rows; i++) {
					//获取列数
					 row = sheet.getRow(i);
					for (int j = 0; j < columns; j++) {
						String cell = row.getCell(j).toString();
						blh[i][j]=Double.valueOf(cell);
					}
				}
				return blh;
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				fis.close();
			}

			return blh;
		}
	public double[][] getNumber2() throws Exception{
		FileInputStream fis = null;
		double[][] blh=null;
		try {
			File file;
			fis = new FileInputStream("qqai.xlsx");
			XSSFWorkbook sheets = new XSSFWorkbook(fis);
			XSSFSheet sheet = sheets.getSheet("Sheet1");
			int rows = sheet.getPhysicalNumberOfRows();
			XSSFRow row = sheet.getRow(1);
			int columns = row.getPhysicalNumberOfCells();
			System.out.println(rows);
			blh=new double[rows][columns];
			for (int i = 0; i < rows; i++) {
				//获取列数
				row = sheet.getRow(i);
				for (int j = 0; j < columns; j++) {
					String cell = row.getCell(j).toString();
					blh[i][j]=Double.valueOf(cell);
				}
			}
			return blh;
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			fis.close();
		}

		return blh;
	}

	public void write(double tt[][]) throws Exception{
		XSSFWorkbook wb = new XSSFWorkbook();
		// 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
		XSSFSheet sheet = wb.createSheet("11");
		// 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
		XSSFRow row=null;
		int len=tt.length;
		for(int i=0;i<len;i++){
			row= sheet.createRow((i));
			for(int j=0;j<tt[0].length;j++){
				Cell cell=row.createCell(j);
				cell.setCellValue(tt[i][j]);


			}
		}
		FileOutputStream out = new FileOutputStream(new File("D:/11.xlsx"));
		wb.write(out);
		out.close();
		wb.close();
		System.out.println("end");
	}

}
